home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Taiji Applet Pack v2.7 / RandomLink / RandomLink.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-11-06  |  8.8 KB  |  385 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Cursor;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.MediaTracker;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.net.URLEncoder;
  16.  
  17. public class RandomLink extends Applet implements Runnable, MouseListener {
  18.    private Image image;
  19.    // $FF: renamed from: wi int
  20.    private int field_0;
  21.    // $FF: renamed from: he int
  22.    private int field_1;
  23.    private int decx;
  24.    private int decy;
  25.    private int n_link;
  26.    private int link_number;
  27.    private Thread thread;
  28.    // $FF: renamed from: b java.awt.Graphics
  29.    private Graphics field_2;
  30.    private Image buffer;
  31.    private int wi_im;
  32.    private int he_im;
  33.    private boolean loaded;
  34.    private double[] pourcent;
  35.    private String image_name;
  36.    private String url;
  37.    private String statusBarText;
  38.    private AudioClip sound;
  39.    private AudioClip clicSound;
  40.    private AudioClip enterSound;
  41.    private boolean loopSound;
  42.    // $FF: renamed from: fm java.awt.FontMetrics
  43.    private FontMetrics field_3;
  44.    private String enterText;
  45.    private boolean isEnterTextEnabled;
  46.    private int wiEnterText;
  47.    private int enterTextHeight;
  48.    private Color enterTextColor;
  49.  
  50.    public String getAppletInfo() {
  51.       return "Name: RandomLink\r\nAuthor: Taiji Software\r\n";
  52.    }
  53.  
  54.    public RandomLink() {
  55.       ((Component)this).addMouseListener(this);
  56.    }
  57.  
  58.    public void register() {
  59.       try {
  60.          URL u = new URL("http://www.taijisoftware.com");
  61.          ((Applet)this).getAppletContext().showDocument(u, "_blank");
  62.       } catch (Exception e) {
  63.          System.out.println(e);
  64.          this.stop();
  65.       }
  66.    }
  67.  
  68.    public void init() {
  69.       String codeBase = null;
  70.  
  71.       try {
  72.          codeBase = (new URL(((Applet)this).getCodeBase().toString())).getHost();
  73.          System.out.println("Copyright 1999, 2001 Taiji Software(tm)\nYour domain name is : " + codeBase);
  74.          codeBase = codeBase.toUpperCase();
  75.       } catch (Exception var9) {
  76.       }
  77.  
  78.       if (!((Applet)this).getCodeBase().toString().toUpperCase().startsWith("FILE") || ((Applet)this).getParameter("debug") != null) {
  79.          String regCode = ((Applet)this).getParameter("registration_code");
  80.          if (regCode == null) {
  81.             regCode = ((Applet)this).getParameter("reg_domain");
  82.             if (regCode == null) {
  83.                this.register();
  84.             } else {
  85.                if (regCode.length() == codeBase.length() + 4 && !codeBase.startsWith("WWW.")) {
  86.                   codeBase = "WWW." + codeBase;
  87.                } else if (regCode.length() == codeBase.length() - 4 && codeBase.startsWith("WWW.")) {
  88.                   codeBase = codeBase.substring(4);
  89.                }
  90.  
  91.                char[] chars = new char[codeBase.length()];
  92.                codeBase.getChars(0, codeBase.length(), chars, 0);
  93.                String key = new String("haricot");
  94.                char[] chars2 = new char[key.length()];
  95.                key.getChars(0, key.length(), chars2, 0);
  96.  
  97.                for(int i = 0; i < codeBase.length(); ++i) {
  98.                   int j;
  99.                   if (i >= key.length()) {
  100.                      j = i - key.length() * (i / key.length());
  101.                   } else {
  102.                      j = i;
  103.                   }
  104.  
  105.                   chars[i] += chars2[j];
  106.                   chars[i] = (char)(chars[i] - chars[i] / 26 * 26 + 97);
  107.                }
  108.  
  109.                String res = new String(chars);
  110.                if (!res.equalsIgnoreCase(regCode)) {
  111.                   this.register();
  112.                }
  113.             }
  114.          } else if (!regCode.equals("settevercsedegnamiaj") && !regCode.equals("8078")) {
  115.             this.register();
  116.          }
  117.       }
  118.  
  119.       this.getParameters();
  120.       if (this.sound != null) {
  121.          if (this.loopSound) {
  122.             this.sound.loop();
  123.             return;
  124.          }
  125.  
  126.          this.sound.play();
  127.       }
  128.  
  129.    }
  130.  
  131.    public void getParameters() {
  132.       String s = ((Applet)this).getParameter("link_number");
  133.       if (s != null) {
  134.          this.link_number = Integer.parseInt(s);
  135.       }
  136.  
  137.       this.field_0 = ((Component)this).getSize().width;
  138.       this.field_1 = ((Component)this).getSize().height;
  139.       ((Component)this).setBackground(this.getColor("background_color"));
  140.       this.image_name = ((Applet)this).getParameter("image");
  141.       this.pourcent = new double[this.link_number + 1];
  142.  
  143.       for(int i = 1; i <= this.link_number; ++i) {
  144.          String var3 = null;
  145.          String var4 = ((Applet)this).getParameter("pourcent" + Integer.toString(i));
  146.          if (var4 != null) {
  147.             this.pourcent[i] = (double)Integer.parseInt(var4);
  148.          } else {
  149.             this.pourcent[i] = (double)(100 / this.link_number);
  150.          }
  151.       }
  152.  
  153.       this.statusBarText = ((Applet)this).getParameter("status_bar_text");
  154.       s = ((Applet)this).getParameter("sound_name");
  155.       if (s != null) {
  156.          this.sound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  157.       }
  158.  
  159.       s = ((Applet)this).getParameter("loop_sound");
  160.       if (s != null) {
  161.          if (s.equals("yes")) {
  162.             this.loopSound = true;
  163.          } else {
  164.             this.loopSound = false;
  165.          }
  166.       }
  167.  
  168.       s = ((Applet)this).getParameter("clic_sound_name");
  169.       if (s != null) {
  170.          this.clicSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  171.       }
  172.  
  173.       s = ((Applet)this).getParameter("enter_sound_name");
  174.       if (s != null) {
  175.          this.enterSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  176.       }
  177.  
  178.       this.enterText = ((Applet)this).getParameter("enter_text");
  179.       s = ((Applet)this).getParameter("enter_text_height");
  180.       if (s != null) {
  181.          this.enterTextHeight = Integer.parseInt(s);
  182.       }
  183.  
  184.       this.enterTextColor = this.getColor("enter_text_color");
  185.       if (this.enterTextColor == null) {
  186.          this.enterTextColor = Color.white;
  187.       }
  188.  
  189.    }
  190.  
  191.    public Color getColor(String param) {
  192.       String s = ((Applet)this).getParameter(param);
  193.       if (s != null) {
  194.          if (s.substring(0, 1).equals("#")) {
  195.             s = s.substring(1);
  196.             int i = Integer.parseInt(s, 16);
  197.             return new Color(i);
  198.          } else {
  199.             return null;
  200.          }
  201.       } else {
  202.          return null;
  203.       }
  204.    }
  205.  
  206.    public void start() {
  207.       if (this.thread == null) {
  208.          this.thread = new Thread(this);
  209.          this.thread.start();
  210.       }
  211.  
  212.    }
  213.  
  214.    public void stop() {
  215.       if (this.thread != null) {
  216.          this.thread = null;
  217.       }
  218.  
  219.       if (this.sound != null) {
  220.          this.sound.stop();
  221.       }
  222.  
  223.    }
  224.  
  225.    public final void paint(Graphics g) {
  226.       if (this.loaded) {
  227.          this.field_2.clearRect(0, 0, this.field_0, this.field_1);
  228.          this.field_2.drawImage(this.image, this.decx, this.decy, this);
  229.          if (this.isEnterTextEnabled) {
  230.             this.field_2.drawString(this.enterText, (this.field_0 - this.wiEnterText) / 2, this.field_1 / 2);
  231.          }
  232.  
  233.          g.drawImage(this.buffer, 0, 0, this);
  234.       }
  235.  
  236.    }
  237.  
  238.    public void run() {
  239.       if (!this.loaded) {
  240.          MediaTracker tracker = new MediaTracker(this);
  241.          this.image = ((Applet)this).getImage(((Applet)this).getCodeBase(), this.image_name);
  242.          tracker.addImage(this.image, 0);
  243.  
  244.          try {
  245.             tracker.waitForAll();
  246.             this.loaded = !tracker.isErrorAny();
  247.          } catch (InterruptedException var5) {
  248.             this.stop();
  249.          }
  250.  
  251.          if (!this.loaded) {
  252.             this.stop();
  253.             return;
  254.          }
  255.  
  256.          this.wi_im = this.image.getWidth(this);
  257.          this.he_im = this.image.getHeight(this);
  258.          this.decx = (this.field_0 - this.wi_im) / 2;
  259.          this.decy = (this.field_1 - this.he_im) / 2;
  260.          this.buffer = ((Component)this).createImage(this.field_0, this.field_1);
  261.          this.field_2 = this.buffer.getGraphics();
  262.          this.field_2.setColor(this.enterTextColor);
  263.          String fontName = ((Applet)this).getParameter("enter_text_font");
  264.          if (fontName == null) {
  265.             fontName = "TimesRoman";
  266.          }
  267.  
  268.          int style = 1;
  269.          String s = ((Applet)this).getParameter("enter_text_style");
  270.          if (s != null) {
  271.             if (s.equals("bold")) {
  272.                style = 1;
  273.             } else if (s.equals("italic")) {
  274.                style = 2;
  275.             } else if (s.equals("bold_italic")) {
  276.                style = 3;
  277.             } else {
  278.                style = 0;
  279.             }
  280.          }
  281.  
  282.          this.field_2.setFont(new Font(fontName, style, this.enterTextHeight));
  283.          this.field_3 = this.field_2.getFontMetrics();
  284.          if (this.enterText != null) {
  285.             this.wiEnterText = this.field_3.stringWidth(this.enterText);
  286.          }
  287.       }
  288.  
  289.       ((Component)this).repaint();
  290.    }
  291.  
  292.    public URL giveURL(String url) {
  293.       try {
  294.          if (url.toUpperCase().startsWith("HTTP")) {
  295.             return new URL(url);
  296.          } else if (url.toUpperCase().startsWith("FTP")) {
  297.             int p = url.indexOf(":");
  298.             int p2 = url.indexOf(":", p + 1);
  299.             if (p2 != -1) {
  300.                url = url.substring(0, p + 3) + URLEncoder.encode(url.substring(p + 3, url.length()));
  301.             }
  302.  
  303.             p = url.indexOf("%40");
  304.             if (p != -1) {
  305.                url = url.substring(0, p) + "@" + url.substring(p + 3, url.length());
  306.             }
  307.  
  308.             return new URL(url);
  309.          } else {
  310.             return new URL(((Applet)this).getCodeBase(), url);
  311.          }
  312.       } catch (MalformedURLException e) {
  313.          System.out.println(e);
  314.          return null;
  315.       }
  316.    }
  317.  
  318.    public void mouseClicked(MouseEvent e) {
  319.    }
  320.  
  321.    public void mouseEntered(MouseEvent e) {
  322.       if (this.enterText != null) {
  323.          this.isEnterTextEnabled = true;
  324.          ((Component)this).repaint();
  325.       }
  326.  
  327.       if (this.statusBarText != null) {
  328.          ((Applet)this).showStatus(this.statusBarText);
  329.       }
  330.  
  331.       if (this.enterSound != null) {
  332.          this.enterSound.play();
  333.       }
  334.  
  335.       ((Component)this).setCursor(new Cursor(12));
  336.    }
  337.  
  338.    public void mouseExited(MouseEvent e) {
  339.       if (this.isEnterTextEnabled) {
  340.          this.isEnterTextEnabled = false;
  341.          ((Component)this).repaint();
  342.       }
  343.  
  344.       ((Component)this).setCursor(new Cursor(0));
  345.    }
  346.  
  347.    public void mousePressed(MouseEvent e) {
  348.       if (this.clicSound != null) {
  349.          this.clicSound.play();
  350.       }
  351.  
  352.       double r = (double)100.0F * Math.random();
  353.       double t = (double)0.0F;
  354.  
  355.       for(int i = 1; i <= this.link_number; ++i) {
  356.          t += this.pourcent[i];
  357.          if (r < t) {
  358.             this.n_link = i;
  359.             break;
  360.          }
  361.       }
  362.  
  363.       if (this.n_link < 1 || this.n_link > this.link_number) {
  364.          this.n_link = (int)((double)this.link_number * Math.random()) + 1;
  365.       }
  366.  
  367.       this.url = ((Applet)this).getParameter("link" + Integer.toString(this.n_link));
  368.  
  369.       try {
  370.          URL u = this.giveURL(this.url);
  371.          String target = ((Applet)this).getParameter("target");
  372.          if (target == null) {
  373.             target = "_self";
  374.          }
  375.  
  376.          ((Applet)this).getAppletContext().showDocument(u, target);
  377.       } catch (Exception err) {
  378.          System.err.println(err);
  379.       }
  380.    }
  381.  
  382.    public void mouseReleased(MouseEvent e) {
  383.    }
  384. }
  385.